package com.real8.romemp.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.real8.romemp.client.GameMessages;

public class PoliticalViewImpl extends Composite implements PoliticalView {

	private static PoliticalViewImplUiBinder uiBinder = GWT
			.create(PoliticalViewImplUiBinder.class);

	interface PoliticalViewImplUiBinder extends
			UiBinder<Widget, PoliticalViewImpl> {
	}

	private Presenter presenter;

	@UiField
	HTMLPanel politicsPanel;

	@UiField
	HTMLPanel popularPanel;

	@UiField
	HTMLPanel notPopularPanel;
	@UiField
	SpanElement neededPopularityPointsSpan;

	@UiField
	QuestionAndAnswerView appeaseQAView;

	@UiField
	Button continueButton;

	public PoliticalViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;

		appeaseQAView.setPresenter(new QuestionAndAnswerView.Presenter() {
			@Override
			public void onAnswer(int answer) {
				presenter.onAppeaseAnswer(answer);
			}
		});
	}

	@Override
	public void hideAll() {
		politicsPanel.setVisible(false);
		popularPanel.setVisible(false);
		notPopularPanel.setVisible(false);
		appeaseQAView.setVisible(false);
		continueButton.setVisible(false);
	}

	@Override
	public void showPolitics() {
		politicsPanel.setVisible(true);
	}

	@Override
	public void showPopular() {
		popularPanel.setVisible(true);
	}

	@Override
	public void showNotPopular(int neededPopularityPoints) {
		neededPopularityPointsSpan.setInnerText("" + neededPopularityPoints);

		notPopularPanel.setVisible(true);
	}

	@Override
	public void showAppeaseQ() {
		GameMessages messages = (GameMessages) GWT.create(GameMessages.class);

		appeaseQAView.showQuestion(messages.buyHowMuchPopularityPoints());
	}

	@Override
	public void showContinue() {
		continueButton.setVisible(true);
	}

	@UiHandler("continueButton")
	void onContinueButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onContinue();
		}
	}
}
